package com.example.agenda.presenter;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

import com.example.agenda.MainActivity;
import com.example.agenda.view.EditContactView;
import com.example.util.JSONParser;
import com.example.vo.Contact;

public class EditContactPresenter {

	EditContactView editContactView;
	private Context mContext;

	public EditContactPresenter(EditContactView editContactView, Context context) {
		this.editContactView = editContactView;
		this.mContext = context;
	}

	public void start() {
		bind();

		String id = editContactView.getId();

		if (id != null && !"".equals(id)) {
			Contact contact = getContact(id);
			editContactView.getNomeText().setText(contact.getFirstName());
			editContactView.getSegundoNomeText().setText(contact.getLastName());
			editContactView.getEmailText().setText(contact.getEmailAddress());
		} else {
			editContactView.getNomeText().setText("");
			editContactView.getSegundoNomeText().setText("");
			editContactView.getEmailText().setText("");
		}
	}

	private void bind() {
		editContactView.getButtontSave().setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						doSave();
					}
				});

		editContactView.getButtontCancel().setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(mContext, MainActivity.class);
						mContext.startActivity(intent);
					}
				});
	}

	private void doSave() {
		String id = editContactView.getId();

		JSONParser jsonParser = new JSONParser();

		Contact contact = new Contact(id, editContactView.getNomeText()
				.getText().toString(), editContactView.getSegundoNomeText()
				.getText().toString(), editContactView.getEmailText().getText()
				.toString());

		String json = jsonParser.getJSONObjectFromObject(contact);

		HttpClient httpClient = new DefaultHttpClient();
		HttpContext localContext = new BasicHttpContext();

		if (id != null && !"".equals(id)) {
			try {
				HttpPut httpPut = new HttpPut(
						"http://10.0.2.2:8080/Agenda-service/contacts/");

				httpPut.setEntity(new StringEntity(json));
				httpPut.setHeader("Accept", "application/json");
				httpPut.setHeader("Content-type", "application/json");
				httpClient.execute(httpPut, localContext);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			try {
				HttpPost httpPost = new HttpPost(
						"http://10.0.2.2:8080/Agenda-service/contacts/");

				httpPost.setEntity(new StringEntity(json));
				httpPost.setHeader("Accept", "application/json");
				httpPost.setHeader("Content-type", "application/json");
				httpClient.execute(httpPost, localContext);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		Intent intent = new Intent(mContext, MainActivity.class);
		mContext.startActivity(intent);
	}

	private Contact getContact(String id) {
		HttpClient httpClient = new DefaultHttpClient();
		HttpContext localContext = new BasicHttpContext();
		HttpGet httpGet = new HttpGet(
				"http://10.0.2.2:8080/Agenda-service/contacts/" + id);
		Contact contact = null;
		try {
			HttpResponse response = httpClient.execute(httpGet, localContext);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();

			JSONParser jSONParser = new JSONParser();
			JSONObject jsonObject = jSONParser.getJSONObjectFromString(is);

			contact = new Contact(jsonObject.getString("id"),
					jsonObject.getString("firstName"),
					jsonObject.getString("lastName"),
					jsonObject.getString("emailAddress"));

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return contact;
	}
}
